/**
 * Created by dylan_Apple on 17/6/25.
 */
var async = require('async');
var http=require('http');
var stockUtil = require('../utils/stock');
var stockRecommend = require('../controller/controller.stockrecommend');
var comment = require('../controller/controller.comment');
var seller = require('../controller/controller.seller');
var user = require('../controller/controller.user');

module.exports = {
    //初始化个股推荐
    initStockRecommend: function (req, res, next) {
        var params = req.query;
        var that = this;
        async.auto({
            seller: ['mConference',function (results,callback) {
                //var role = req.session.userinfo.role;
                //if (role == '4' || role == '5') {
                //    params.rowId = req.session.userinfo.seller.rowId;
                //}
                if (results && results.mConference){
                    params.rowId = results.mConference.seller.rowId;
                }else if (params != null && params.addType == '0'){ //内部推荐
                    params.rowId = req.session.userinfo.seller.rowId;
                }
                seller.getEnabledSellerList(params, function (err, result) {
                    if (err) {
                        console.error("seller list query error:" , err);
                        callback(err);
                    } else {
                        callback(null, result);
                    }
                });
            }],
            person: ['mConference',function (results,callback) {
                if (results && results.mConference){
                    params.seller = results.mConference.seller.rowId;
                }else if (params != null && params.addType == '0'){ //内部推荐
                    params.seller = req.session.userinfo.seller.rowId;
                }
                user.getUserListBySeller(params, function (err, result) {
                    if (err) {
                        console.error("user list query error:" , err);
                        callback(err);
                    } else {
                        callback(null, result);
                    }
                });
            }],
            mConference:function (callback) {
                if(req.query.rowId){
                    stockRecommend.getInfo({rowId:req.query.rowId}, function (err, result) {
                        callback(err, result && result.obj);
                    });
                }else{
                    callback();
                }
            }
        }, function (err, results) {
            if (err) {
                console.error("initStockRecommend error:" , err);
                res.redirect('/error');
                return;
            } else {
                if (params.addType!=undefined)
                   results.addType = params.addType;
                next(results);
            }
        });
    },
    //创建个股推荐
    createOrUpdateStockRecommend: function (req, res, next) {
        var params = req.body||{};
        params.actionType = "stockrecommend";
        params.createUid = req.session.uid;
        params.isRecommend = '1';
        var daoMethod = stockRecommend[params.rowId?'update':'create'];
        daoMethod(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next({
                'stockrecommend': result.obj
            });
        });
    },
    //获取个股推荐详情
    getStockRecommendInfo: function (req, res, next) {
        var params = req.query;
        async.parallel({
            commentList:function(done){
                params.type = 'stockrecommend';
                comment.getListByMconference(params, function (err, result) {
                    done(err,result && result.obj);
                });
            },
            mConference:function(done){
                stockRecommend.getInfo(params, function (err, result) {
                    if (err) {
                        res.redirect('/error');
                        return;
                    }
                    done(err, result && result.obj);
                });
            }
        },function(error,results){
            next(results);
        });
    },
    //获取个股推荐列表
    getOldStockRecommendList: function (req, res, next) {
        var params = req.query;
        if (params.queryStr == null) {
            params.queryStr = "";
        }
        params.actionType = "stockrecommend";
        params.isRecommend = '1';
        params.enable = '1';
        stockRecommend.getListByActionType(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next({
                    'mconferencelist': result.obj,
                    moment: require("moment")
                }
            );
        })
    },
    //金股、股票池收益率统计
    getStockCount:function(req,res,next){
        var params = req.query;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        stockRecommend.getStockCount(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result.obj);
            }
        })
    },
    //删除个股推荐
    delteStockRecommendList: function (req, res, next) {
        var params = req.body;
        async.waterfall([
            function (done) {
                stockRecommend.getInfo(params, function (err, result) {
                    if (err) {
                        next({'code': 404, 'message': "调出错误"});
                        return;
                    }
                    done(err, result && result.obj);
                });
            },
            function (stockInfo, done) {
                stockUtil.getRealRate(stockInfo.importPrice,stockInfo.stockId.stockCode,function (err, realRate) {
                    done(err, realRate);
                });
            },
            function (realRate,done) {
                params.realRate = realRate;
                stockRecommend.deleteStockRecommend(params, function (err, result) {
                    done(err, result);
                });
            }
        ], function (error, result) {
            if (error){
                next({code: 404, message: '调出错误'})
            }else{
                next({code: 200, message: '调出成功'});
            }
        });
    }


};

